package com.zzty.txj1.controller;

import com.zzty.txj1.entity.Sport;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.SportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sport")
public class SportController {
    private static final Logger logger = LoggerFactory.getLogger(SportController.class);

    @Autowired
    SportService sportService;

    @RequestMapping("/getUserId")
    public Map getOpenId(HttpServletRequest request){
        logger.info("sport--controller ============ getUserId");

        User user =(User)(request.getSession().getAttribute("user"));
        Integer userId=Integer.valueOf(user.getId());
        logger.info("sport--controller ============ userId is : "+userId);
        Map<String,Integer> map=new HashMap<>();
        map.put("userId",userId);
        return map;
    }

    @RequestMapping(value = "/runStart",method = RequestMethod.POST)
    public Integer runStart(Sport sport){
        logger.info("sport--controller ============ runStart");

        logger.info("sport--controller ============controller in :"+sport.toString());

        return null;
    }

    @RequestMapping("/getSportByUserIdAndDate")
    public Map<String, Object> getSportByUserIdAndDate(Sport sport){
        logger.info("sport--controller ============ getSportByUserIdAndDate");

        Map<String,Object> results=new HashMap<>();
       String result="false";
        Sport sport1=sportService.getSportByUserIdAndDate(sport);
        logger.info("sport--controller ============sport1"+sport1);
       if(sport1!=null){
           result="success";
       }
        results.put("result",result);
        results.put("sport",sport1);
        //results.put("result",result);
        return results;
    }

    @RequestMapping(value = "/initInformation",method = RequestMethod.POST)
    public Integer initInformation(Sport sport){
        logger.info("sport--controller ============ initInformation");

        Integer flag=sportService.initInformation(sport);
        return flag;
    }
    @RequestMapping(value = "/updateStartTime",method = RequestMethod.POST)
    public Integer updateStartTime(Sport sport){
        logger.info("sport--controller ============ updateStartTime");

        Integer count=sportService.updateStartTime(sport);
        return count;
    }
    @RequestMapping(value = "/updateMiddleTime",method = RequestMethod.POST)
    public Integer updateMiddleTime(Sport sport){
        logger.info("sport--controller ============ updateMiddleTime");

        Integer count=sportService.updateMiddleTime(sport);
        return count;
    }
    @RequestMapping(value = "/updateFinishTime",method = RequestMethod.POST)
    public Integer updateFinishTime(Sport sport){
        logger.info("sport--controller ============ updateFinishTime");

        Integer count=sportService.updateFinishTime(sport);
        return count;
    }
    @RequestMapping(value = "/updateSpendTime",method = RequestMethod.POST)
    public Integer updateSpendTime(Sport sport){
        logger.info("sport--controller ============ updateSpendTime");

        Integer count=sportService.updateSpendTime(sport);
        return count;
    }


}
